package com.cskaoyan;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * @author stone
 * @date 2022/06/16 17:40
 */
@Component
public class CustomBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("BeanPostProcessor的before：" + beanName);
        if (bean instanceof DruidDataSource) {
            String password = ((DruidDataSource) bean).getPassword();
            String processedPassword = "123456";
            ((DruidDataSource) bean).setPassword(processedPassword);
            return bean;
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("BeanPostProcessor的after:" + beanName);
        return bean;
    }
}
